﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Models;
using DAL;
using System.Web.Script.Serialization;

namespace 公司CRM客户关系系统.Sale
{
    /// <summary>
    /// SaleChanceHandler 的摘要说明
    /// </summary>
    public class SaleChanceHandler : IHttpHandler,System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //得到action
            string action = context.Request.Form["action"];
            //创建jss对象
            JavaScriptSerializer jss = new JavaScriptSerializer();
            if (action == "add")
            {
                //添加销售机会表
                //接收提交的数据
                Chances ch = new Chances();
                ch.ChanName = context.Request.Form["ChanName"];
                ch.ChanRate = int.Parse(context.Request.Form["ChanRate"]);
                ch.ChanLinkMan = context.Request.Form["ChanLinkMan"];
                ch.ChanLinkTel = context.Request.Form["ChanLinkTel"];
                ch.ChanTitle = context.Request.Form["ChanTitle"];
                ch.ChanDesc = context.Request.Form["ChanDesc"];
                ch.ChanCreateDate = DateTime.Now;
                ch.ChanCreateMan = Convert.ToInt32((context.Session["user"] as V_Users).UserID);
                
                ch.ChanState = Convert.ToInt32(1);
                ch.ChanError=null;
                //添加到数据库
                int r = BaseDAL.Insert(ch);
                //返回结果
                context.Response.Write(r);
            }
            else if (action == "update")
            {
                //获取要修改的 ChanID
                int ChanID = int.Parse(context.Request.Form["ChanID"]);
                //接收提交的数据
                Chances ch = new Chances();
                ch.ChanID = ChanID;
                ch.ChanName = context.Request.Form["ChanName"];
                ch.ChanRate = int.Parse(context.Request.Form["ChanRate"]);
                ch.ChanLinkMan = context.Request.Form["ChanLinkMan"];
                ch.ChanLinkTel = context.Request.Form["ChanLinkTel"];
                ch.ChanTitle = context.Request.Form["ChanTitle"];
                ch.ChanDesc = context.Request.Form["ChanDesc"];
                //从数据库查出来的值(不需要修改)
                Chances chan = BaseDAL.Find<Chances>(ChanID);
                ch.ChanCreateDate = chan.ChanCreateDate;
                ch.ChanCreateMan = chan.ChanCreateMan;
                ch.ChanDueMan = chan.ChanDueMan;
                ch.ChanDueDate = chan.ChanDueDate;
                ch.ChanState = chan.ChanState;
                ch.ChanError = chan.ChanError;
                //修改数据库
                int r = BaseDAL.Update(ch);
                //返回结果
                context.Response.Write(r);
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}